﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RSAEncryptionLib;
using DataTransferObjects;

namespace Business
{
    public class rsaBUS
    {        
        static public byte[] encryptUsePubKey(int iReceiverID, byte[] encryptByte)
        {

            RSAEncryption rsa = new RSAEncryption();

            KeyInforDTO keyReceiverInfor = KeyBUS.getKeyInforFromDB(iReceiverID);
            rsa.importPublicKey(keyReceiverInfor.rsaExponent, keyReceiverInfor.rsaModulus);
            return rsa.PublicEncryption(encryptByte);
        }

        static public byte[] encryptUsePubKey(string strEmailReceiver, byte[] encryptByte)
        {

            RSAEncryption rsa = new RSAEncryption();

            AccountInforDTO accountInfor = AccountBUS.getAccInforFromDB(strEmailReceiver);
            KeyInforDTO keyReceiverInfor = KeyBUS.getKeyInforFromDB(accountInfor.iUserID);
            rsa.importPublicKey(keyReceiverInfor.rsaExponent, keyReceiverInfor.rsaModulus);
            return rsa.PublicEncryption(encryptByte);
        }

        static public byte[] decryptUsePrivKey(int iUserID, byte[] encryptedByte)
        {

            RSAEncryption rsa = new RSAEncryption();

            KeyInforDTO keyInfor = KeyBUS.getKeyInforFromDB(iUserID);
            rsa.importPrivateKey(keyInfor.rsaExponent, keyInfor.rsaModulus, keyInfor.rsaD);
            return rsa.PrivateDecryption(encryptedByte);
        }
    }
}
